package com.settlement.system.controller;

import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.enums.MessageTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 上传文件表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-11-17
 */
@RestController
@RequestMapping("/api/v1/uploadFile")
@RequiredArgsConstructor
public class SysUploadFileController {


    // 保存文件的本地目录
    @Value("${system.config.filePath}")
    private String saveFilePath;

    @Operation(summary = "下载文件")
    @PreventDuplicateSubmit
    @GetMapping("/downloadFile/{id}/{type}")
    public void downloadFile(@PathVariable Long id, @PathVariable String type, HttpServletResponse response) throws Exception {
        /*SysUploadFile uploadFile = sysUploadFileService.findById(id, MessageTypeEnum.valueOf(type));
        File file = new File(saveFilePath, uploadFile.getFilePath());
        // 将文件写入输入流
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStream fis = new BufferedInputStream(fileInputStream);
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(uploadFile.getFileName(), StandardCharsets.UTF_8));
        // 告知浏览器文件的大小
        response.addHeader("Content-Length", file.length() + "");
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();*/
    }
}
